/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amse.grigory.dchess.io;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import org.amse.grigory.dchess.dchess.DChess;
import org.amse.grigory.dchess.view.Theme;

/**
 *
 * @author grigory
 */
public class ThemeLoader {
    private static ClassLoader ourDClass= DChess.class.getClassLoader();

    public static void loadTheme(Theme theme, String themeFile) throws IOException {
        // Reading info from theme file        
        Scanner scanner = new Scanner(ourDClass.getResourceAsStream(themeFile));        
        loadColors(scanner, theme);        
        loadFigures(scanner, theme);        
        scanner.close();        
    }

    private static void loadColors(Scanner scanner, Theme theme) {
        scanner.next();
        int n = Integer.valueOf(scanner.next()).intValue();
        theme.newColors(n);
        for (int i = 0; i < n; ++i) {
            scanner.next();
            int r = Integer.valueOf(scanner.next()).intValue();
            int g = Integer.valueOf(scanner.next()).intValue();
            int b = Integer.valueOf(scanner.next()).intValue();
            theme.setColor(new Color(r, g, b), i);
        }
        scanner.next();
        int r = Integer.valueOf(scanner.next()).intValue();
        int g = Integer.valueOf(scanner.next()).intValue();
        int b = Integer.valueOf(scanner.next()).intValue();
        theme.setIllegalColor(new Color(r, g, b));
        scanner.next();
        if (scanner.next().equals("false")) {
            return;
        } else {
            scanner.next();
            r = Integer.valueOf(scanner.next()).intValue();
            g = Integer.valueOf(scanner.next()).intValue();
            b = Integer.valueOf(scanner.next()).intValue();
            theme.setFigureColor(new Color(r, g, b));
            scanner.next();
            r = Integer.valueOf(scanner.next()).intValue();
            g = Integer.valueOf(scanner.next()).intValue();
            b = Integer.valueOf(scanner.next()).intValue();
            theme.setMoveColor(new Color(r, g, b));
            scanner.next();
            r = Integer.valueOf(scanner.next()).intValue();
            g = Integer.valueOf(scanner.next()).intValue();
            b = Integer.valueOf(scanner.next()).intValue();
            theme.setAttackColor(new Color(r, g, b));
            scanner.next();
            r = Integer.valueOf(scanner.next()).intValue();
            g = Integer.valueOf(scanner.next()).intValue();
            b = Integer.valueOf(scanner.next()).intValue();
            theme.setIndicationColor(new Color(r, g, b));
        }

    }

    private static void loadFigures(Scanner scanner, Theme theme) throws IOException {
        String temp = scanner.next();
        while (true) {
            String name = scanner.next();
            if (name.equals("Black")) {
                break;
            }
            temp = scanner.next();
            BufferedImage img = ImageIO.read(ourDClass.getResourceAsStream(temp));
            theme.putWhiteImg(name, img);
        }

        while (scanner.hasNext()) {
            String name = scanner.next();
            temp = scanner.next();
            BufferedImage img = ImageIO.read(ourDClass.getResourceAsStream(temp));
            theme.putBlackImg(name, img);
        }
    }
}
